package com.jinmdz.app.api.wrapper;

import com.jinmdz.fmis.core.base.BaseBean;
import com.jinmdz.fmis.core.constant.Constants;
import com.jinmdz.fmis.core.util.JacksonUtil;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * HttpServletResponse封装处理类
 *
 * @author LiCongLu
 * @date 2019-12-20 09:59
 */
@Component
public class ResponseWrapper {

    /**
     * 请求响应返回Json字符串
     *
     * @param response 响应对象
     * @param bean     返回数据实体
     * @return
     * @author LiCongLu
     * @date 2019-12-20 10:04
     */
    public <T extends BaseBean> void json(HttpServletResponse response, T bean) {
        try {
            //设置编码格式
            response.setContentType("text/plain;charset=" + Constants.UTF8);
            response.setCharacterEncoding(Constants.UTF8);

            try {
                PrintWriter writer = response.getWriter();
                writer.write(JacksonUtil.obj2Json(bean));
                writer.flush();
            } catch (IOException e) {
                // e.printStackTrace();
            }

        } catch (Exception e) {
            // e.printStackTrace();
        }
    }
}
